package hu.uszeged.inf.stud.prf.todo.dao.resource;

import hu.uszeged.inf.stud.prf.todo.dao.core.GenericHibernateDAO;
import hu.uszeged.inf.stud.prf.todo.entity.resource.Resource;
import hu.uszeged.inf.stud.prf.todo.entity.user.User;

import java.util.List;

public class ResourceDAO extends GenericHibernateDAO<Resource, Long> {

	public ResourceDAO() {
		super(Resource.class);
	}

	@SuppressWarnings("unchecked")
	public List<Resource> getResourcesForUser(User user) {
		StringBuilder query = new StringBuilder();
		query.append("from Resource res ");
		query.append("where res.user = :user and res.archive=:archive");
		return getSession().createQuery(query.toString())
				.setParameter("user", user)
				.setParameter("archive", false)
				.list();
	}

	public Resource getResourceByName(String resourceName, User user) {
		StringBuilder query = new StringBuilder();
		query.append("from Resource res ");
		query.append("where res.name = :name and res.user = :user and res.archive=:archive");
		return (Resource) getSession().createQuery(query.toString())
				.setParameter("name", resourceName)
				.setParameter("user", user)
				.setParameter("archive", false)
				.uniqueResult();
	}
}
